import { VuexModule, Module, Action, Mutation, getModule } from 'vuex-module-decorators'
import store from '@/store'
import { getToken, setToken, removeToken } from '@/utils/cookies'
import { resetRouter } from '@/router'
import { login, getUserByToken, getUsers, addUser, deleteUser } from '@/api/users'
import { PermissionModule } from './permission'

export interface IUserState {
    token: string
    userName: string
    roleName: string
    roleLevel: number
}

@Module({ dynamic: true, store, name: 'user' })
class User extends VuexModule implements IUserState {
    public token = getToken() || ''
    public userName = ''
    public userNum = ''
    public roleName = ''
    public roleLevel = 0

    @Mutation
    private SET_NAME(userName: string) {
        this.userName = userName
    }
    @Mutation
    private SET_TOKEN(token: string) {
        this.token = token
    }
    @Mutation
    private SET_ROLENAME(roleName: string) {
        this.roleName = roleName
    }
    @Mutation
    private SET_ROLELEVEL(roleLevel: number) {
        this.roleLevel = roleLevel
    }
    @Mutation
    private SET_USERNO(userNum: string) {
        this.userNum = userNum
    }

    @Action({ rawError: true })
    public async Login(userInfo: { userNum: string, password: string }) {
        console.log(userInfo)
        let { userNum, password } = userInfo
        userNum = userNum.trim()
        const { data } = await login({ userNum, password })
        console.log("data", data)
        setToken(data.token)
        this.SET_TOKEN(data.token)
        this.SET_NAME(data.userInfo.userName)
        this.SET_ROLENAME(data.userInfo.roleInfo.roleName)
        this.SET_ROLELEVEL(parseInt(data.userInfo.roleInfo.roleLevel))
        this.SET_USERNO(data.userInfo.userNum)
    }

    @Action({ rawError: true })
    public async GetUserInfo() {
        if (this.token === '') {
            throw Error('获取用户信息错误：未持有token')
        }
        const { data } = await getUserByToken()
        console.log("userinfo", data)
        this.SET_NAME(data.users.userName)
        this.SET_ROLENAME(data.users.roleInfo.roleName)
        this.SET_ROLELEVEL(parseInt(data.users.roleInfo.roleLevel))
        this.SET_USERNO(data.users.userNum)
    }

    // @Action({ rawError: true })
    // public async GetUser(user: { userNum: string, username: string, userrole: string }) {
    //     let { userNum, username, userrole } = user
    //     const { data } = await getUsers({ userNum, username, userrole })
    //     console.log("result", data)
    //     return data.seachResult.users
    // }

    // @Action({ rawError: true })
    // public async AddUser(user: { userNum: string, username: string, sex:string, userrole: string }) {
    //     let { userNum, username, sex, userrole } = user
    //     const { data } = await addUser({ userNum, username, sex, userrole })
    //     console.log("addresult", data)
    //     return data
    // }

    // @Action({ rawError: true })
    // public async DeleteUser(userNum:any) {
    //     console.log("userNum:"+userNum);   
    //     const { data } = await deleteUser({ userNum})
    //     console.log("deleteresult", data)
    //     return data
    // }


    @Action
    public ResetToken() {
        removeToken()
        this.SET_TOKEN('')
    }

    @Action
    public async Logout() {
        if (this.token === '') {
            throw Error('LogOut: token is undefined!')
        }
        // 后端await logout()
        removeToken()
        resetRouter()

        PermissionModule.SetAddRoutesState(false)

        this.SET_TOKEN('')
        this.SET_NAME('')
        this.SET_USERNO('')
        this.SET_ROLENAME('')
        this.SET_ROLELEVEL(0)
    }
}

export const UserModule = getModule(User)